package com.Action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

public class FileProcessAction {
	
	private static final long serialVersionUID = 572146812454l;
//	private static final int BUFFER_SIZE = 1024 * 1024;
	private static final String SUCCESS1 = "success1";
	private static final String SUCCESS2 = "success2";

	
	private Map request = null;
	
	private File myFile;
	private String contentType;
	private String fileName;
	private String imageFileName;
	private String caption;
	
	//get&set

	public void setMyFileContentType(String contentType) {
		this.contentType = contentType;
	}

	public void setMyFileFileName(String fileName) {
		this.fileName = fileName;
	}

	public void setMyFile(File myFile) {
		this.myFile = myFile;
	}

	public String getImageFileName() {
		return imageFileName;
	}

	public String getCaption() {
		return caption;
	}

	public void setCaption(String caption) {
		this.caption = caption;
	}

	//以下为上传文件页面处理程序
	
	private static void copy(File src, File dst) {
		try {
			InputStream in = null;
			OutputStream out = null;
			try {
				in = new BufferedInputStream(new FileInputStream(src));
				out = new BufferedOutputStream(new FileOutputStream(dst));
				// byte [] buffer = new byte [BUFFER_SIZE];
				byte[] buffer = new byte[in.available()];
				while (in.read(buffer) > 0) {
					out.write(buffer);
				}
			} finally {
				if (null != in) {
					in.close();
				}
				if (null != out) {
					out.close();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static String getExtention(String fileName) {
		int pos = fileName.lastIndexOf(".");
		return fileName.substring(pos);
	}

	public String fileUpload() {
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String str = format.format(new Date());
		imageFileName = str + fileName;

		File imageFile = new File(ServletActionContext.getServletContext()
				.getRealPath("/UploadImages")
				+ "/" + imageFileName);
		copy(myFile, imageFile);
		
		request = (Map) ActionContext.getContext().get("request");
		String uploadFileSucc = "恭喜！上传文件成功！";
		request.put("uploadFileSucc",uploadFileSucc);
		
		return SUCCESS1;
	}
	
	
	
	
//以下为文件下载页面处理程序
	
	
	private String downloadFilename;
	
	
	 public void setDownloadFilename(){
         //得到请求下载的文件名
         String fname=ServletActionContext.getRequest().getParameter("name"); 
//         System.out.println("filename1=="+fname); 
         try {
         /*
          * 对fname参数进行UTF-8解码,注意:实际进行UTF-8解码时会使用本地编码，本机为GBK。
          * 这里使用request.setCharacterEncoding解码无效.
          * 只有解码了getDownloadFile()方法才能在下载目录下正确找到请求的文件
          * */  
               fname = new String(fname.getBytes("ISO-8859-1"), "UTF-8");
       } catch (Exception e) {
             e.printStackTrace();
        }  
        this.downloadFilename=fname;  
//        System.out.println("filename2==="+fname); 
    }
 
       /*
        * @getFileName
        * 此方法对应的是struts.xml文件中的：
        * <param name="contentDisposition">attachment;filename="${fileName}"</param>
        * 这个属性设置的是下载工具下载文件时显示的文件名，
        * 要想正确的显示中文文件名，我们需要对fileName再次编码
        * 否则中文名文件将出现乱码，或无法下载的情况
        * */
      public String getDownloadFilename() throws UnsupportedEncodingException {
  
    	  downloadFilename=new String(downloadFilename.getBytes(),"ISO-8859-1");
  
          return downloadFilename;
      }
     /*
       * @getDownloadFile
       * 此方法对应的是struts.xml文件中的：
       * <param name="inputName">downloadFile</param>
       * 返回下载文件的流，可以参看struts2的源码
       * */
      public InputStream getDownloadFile() {
  
         this.setDownloadFilename();
         return ServletActionContext.getServletContext().getResourceAsStream("/UploadImages/" + downloadFilename);
      }
      public String fileDownload() throws Exception {
    	  
          return SUCCESS2;
     }
	

}
